from typing import List

from extract.extractor import Extractor
from src.handle.search import Search
from src.handle.works import Works


class TikTok:
    def __init__(self, parameter):
        self.parameter = parameter
        self.extractor = Extractor(parameter)

    # 根据作品ID获取作品数据
    def input_links_acquisition(
            self,
            tiktok: bool,
            ids: List[str],
            cookie: str = None):
        works_data = [
            Works(
                self.parameter,
                i,
                tiktok,
                cookie).run() for i in ids]
        if not any(works_data):
            raise TypeError("获取作品数据失败")
        return self.extractor.run(works_data)

    # 搜索
    def _deal_search_data(
            self,
            keyword: str,
            type_: tuple,
            pages: int,
            sort: tuple,
            publish: tuple):
        search_data = Search(
            self.parameter,
            keyword,
            type_[0],
            pages,
            sort[0],
            publish[0]).run()
        if not any(search_data):
            print("采集搜索数据失败")
            return None
        return search_data
